package ece.fr.varlet_aimez.monothread;

import java.util.Random;

import ece.fr.varlet_aimez.montecarlo.MonteCarloAbstract;



public class MonteCarloSingle extends MonteCarloAbstract implements Runnable {
	
	
	/**
	 * 
	 * @param CallPutFlag
	 * @param S
	 * @param X
	 * @param T
	 * @param r
	 * @param b
	 * @param v
	 * @param nSteps
	 * @param nSimulations
	 * @return
	 */
	public double MonteCarloStandardOption (String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps, int nSimulations){
		
		double dt, St, Sum=0, Drift, vSqrdt;
		int i, j, z=0;
		
		dt = T / nSteps;
		Drift = (b - java.lang.Math.pow(v, 2) / 2) * dt;
		vSqrdt = v * java.lang.Math.sqrt(dt);
		if (CallPutFlag.equals("c"))
		    z = 1;
		else if (CallPutFlag.equals("p"))
		    z = -1;
		
		for(i=1;i<=nSimulations;i++){
		    St = S;
		    Random rand = new Random();
		    rand.setSeed(System.currentTimeMillis());
		    for(j=0;j<nSteps;j++){
		        St = St * java.lang.Math.exp( Drift + vSqrdt * rand.nextGaussian() );
		    }
		    Sum = Sum + java.lang.Math.max(z*(St - X),0);
		    System.out.println("Etat calcul : "+(i*100)/nSimulations+"%");
		}
		return java.lang.Math.exp(-r * T) * (Sum /nSimulations);
	}
}
